package com.lightjet.macross.base.upms.domain;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.lightjet.macross.common.core.IntKeyBaseEntity;
import io.netty.util.internal.StringUtil;
import io.swagger.annotations.ApiModel;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.apache.commons.lang.StringUtils;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.oauth2.provider.ClientDetails;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;
import java.util.*;
import java.util.stream.Collectors;

@Entity
@Table(name="UPMS_APPCLIENT")
@Getter
@Setter
@NoArgsConstructor
@ApiModel(description = "应用客户端（微服务调用者）实体表")
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler","createDate","updateDate"})
public class AppClient extends IntKeyBaseEntity implements ClientDetails {

    public AppClient(Integer id)
    {
        this.id = id;
    }

    private String clientid;

    private String clientname;

    private String description;

    private String granttypes;

    private String grantedscope;

    private String grantedresources;

    @Column(length = 2000)
    private String grantedauthorities;

    private String secret;

    private int accesstokenspan = 60 * 60 * 24;

    private int refreshtokenspan = 60 * 60 * 24* 30;

    private byte isenable = 1;

    private String entryuri = "";

    private String registeruri = "";

    @Override
    public String getClientId() {
        return clientid;
    }

    public void setClientId(String val){ clientid = val; }

    @Override
    public Set<String> getResourceIds() {
        if(StringUtil.isNullOrEmpty(grantedresources))
        {
            return new HashSet<>();
        }
        else
        {
            return Arrays.stream(grantedresources.split(",")).collect(Collectors.toSet());
        }
    }
    public void setResourceIds(Set<String> val){ grantedresources =  StringUtils.join(val,","); }

    @Override
    public boolean isSecretRequired() {
        return true;
    }

    public void setSecretRequired(boolean val){ }

    @Override
    public String getClientSecret() {
        return secret;
    }
    public void setClientSecret(String val){ secret = val; }

    @Override
    public boolean isScoped() {
        return true;
    }

    public void setScoped(boolean val){ }

    @Override
    public Set<String> getScope() {
        if(StringUtil.isNullOrEmpty(grantedscope))
        {
            return new HashSet<>();
        }
        else
        {
            return Arrays.stream(grantedscope.split(",")).collect(Collectors.toSet());
        }
    }

    public void setScope(Set<String> val)
    {
        grantedscope = StringUtils.join(val,",");
    }

    @Override
    public Set<String> getAuthorizedGrantTypes() {
        if(StringUtil.isNullOrEmpty(granttypes))
        {
            return new HashSet<>();
        }
        else
        {
            HashSet<String> t = new HashSet<>();
            return Arrays.stream(granttypes.split(",")).collect(Collectors.toSet());
        }
    }

    public void setAuthorizedGrantTypes(Set<String> val)
    {
        granttypes = StringUtils.join(val,",");
    }

    @Override
    public Set<String> getRegisteredRedirectUri() {
        if(StringUtil.isNullOrEmpty(registeruri))
        {
            return new HashSet<>();
        }
        else
        {
            return Arrays.stream(registeruri.split(",")).collect(Collectors.toSet());
        }
    }
    public void setRegisteredRedirectUri(Set<String> val)
    {
        registeruri = StringUtils.join(val,",");
    }

    @Override
    public Collection<GrantedAuthority> getAuthorities() {
        return new HashSet<>();
    }

    public void setAuthorities(Collection<GrantedAuthority> val)
    {

    }

    @Override
    public Integer getAccessTokenValiditySeconds() {
        return accesstokenspan;
    }

    public void setAccessTokenValiditySeconds(Integer val)
    {
        accesstokenspan =val;
    }

    @Override
    public Integer getRefreshTokenValiditySeconds() {
        return refreshtokenspan;
    }
    public void setRefreshTokenValiditySeconds(Integer val)
    {
        refreshtokenspan =val;
    }


    @Override
    public boolean isAutoApprove(String scope) {
        return true;
    }
    public void setAutoApprove(boolean val){ }

    @Override
    public Map<String, Object> getAdditionalInformation() {
        return null;
    }
    public void setAdditionalInformation(Map<String, Object> val)
    {

    }
}
